const errorType = require('../constants/error-type');

const errorHandle = (err, ctx) => {
  
  // 错误处理函数
  // ------------------------
  let status, message, ret, errorFile;

  ret = parseInt(err.message.split(' ')[0]);
  errorFile = err.message.split(' ')[1];
  console.log(`[ERROR] file: ${errorFile} ----- errorCode: ${ret}`);

  message = errorType[ret];

  switch (ret) {
    case 2000:
      status = 401; // 未授权
      break;
    default:
      status = 200;
  }

  ctx.status = status;
  ctx.body = {
    ret,
    data: message
  };
  
}

module.exports = {
  errorHandle,
}